<?php

namespace app\admin\dao;

use app\common\model\Role;
use app\common\model\User;
use ShidianUtil\Thinkphp\Dao\DaoTrait;

/**
 * UserDao 数据访问对象
 * Class UserDao
 * @package app\dao
 * @method User findByPK(?int $id, $field = true)
 * @method \think\Collection selectAll(?int $page, ?int $page_size, $field = true)
 * @method User insertActive(array $params, array $allow_field = [])
 * @method User updateActive(User $model, array $params, array $allow_field = [])
 * @method User findAndUpdateActive(int $id, array $params, array $allow_field = [])
 * @method User updateActiveByClosure(\Closure $closure, array $params, array $allow_field = [])
 * @method void deleteModel(User $model)
 * @method void deleteById(int|array|\Closure $id, bool $force = false)
 * @link https://www.kancloud.cn/manual/thinkphp6_0/1037579
 * @link https://www.kancloud.cn/manual/think-orm/1257998
 * @link https://www.kancloud.cn/thinkphp/master-database-and-model/265551
 */
class UserDao
{
    use DaoTrait;

    /**
     * @var User
     */
    private $baseModel;

    /**
     * UserDao constructor.
     */
    public function __construct()
    {
        $this->baseModel = User::class;
    }


    /**
     * 公司列表
     * @param $companyId
     * @param $kw
     * @param int|null $page
     * @param int|null $page_size
     * @return array
     */
    public function listCompanies($companyId, $kw, ?int $page, ?int $page_size): array
    {
        $query = User::when(is_id($companyId), fn($query) => $query->where('id', $companyId))
            ->where('roleType', Role::ROLE_TYPE_COMPANY)
            ->when(not_blank($kw), fn($query) => $query->search('username|name', $kw))
            ->field(true)
            ->withCount([
                'personnels',
                'lawyers' => fn($query) => $query->where('type', '1')
            ])
            ->withSum([
                'transactionOffline',
            ], 'amount')
            ->withSum([
                'accountCard' => fn($query) => $query->where('payMethod', PAYMENT_CARD),
                'accountCoin' => fn($query) => $query->where('payMethod', PAYMENT_COIN),
            ], 'balance')
            ->withAttr([
                'transaction_offline_sum' => fn($value) => $value ?: 0,
                'account_card_sum' => fn($value) => $value ?: 0,
                'account_coin_sum' => fn($value) => $value ?: 0,
            ])
            ->hidden(['token', 'password', 'salt', 'secretKey',])
            ->order('isAdmin', 'DESC')
            ->order('status', 'DESC')
            ->order('id', 'desc')
            ->paging($page, $page_size);

        return listOutput($query, $page_size);
    }


    /**
     * 管理员列表
     * @param int|null $page
     * @param int|null $page_size
     * @return array
     */
    public function listUsers(?int $page, ?int $page_size): array
    {
        $query = User::where('roleType', Role::ROLE_TYPE_ADMIN)
            ->field(true)
            ->hidden(['token', 'password', 'salt', 'secretKey',])
            ->order('isAdmin', 'DESC')
            ->order('status', 'DESC')
            ->order('id', 'desc')
            ->paging($page, $page_size);

        return listOutput($query, $page_size);
    }

    /**
     * 管理员详情
     * @param $id
     * @return User|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function findUser($id): ?User
    {
        return User::where('id', $id)
            ->field(true)
            ->hidden(['token', 'password', 'salt', 'secretKey',])
            ->find();
    }

    /**
     * 前端用户模型
     * @param $id
     * @return User|null
     */
    public function findByLogin($id): ?User
    {
        return User::where('id', $id)
            ->field(true)
            ->hidden(['password', 'salt', 'secretKey',])
            ->find();
    }
}
